#include "head.h"

int lsCommand(command_t* task, user_t* user, int fd) {
    if (task->types == LS) {
        char buf[1000] = {0};
        memset(buf, 0, sizeof(buf));

        // 发送当前路径给服务端
        strcpy(buf, user->path);
//printf("buf:%s\n", buf);
//printf("user_path:%s\n", user->path);
        off_t len = strlen(buf);
//printf("len:%ld\n", len);
//sleep(10);
        send(fd, &len, sizeof(off_t), MSG_NOSIGNAL);
        send(fd, buf, len, MSG_NOSIGNAL);
        //sleep(100);

        // 接收服务端返回的文件或目录名
        while (1) {
            char recv_buf[4096] = {0};
            int bytes_received = recv(fd, recv_buf, sizeof(recv_buf), 0);

            // 如果接收出错或连接关闭，退出循环
            if (bytes_received <= 0) {
                printf("接收结束或连接断开\n");
                break;
            }


            // 打印接收到的文件或目录名
            char* line = strtok(recv_buf, "\n");
            while (line != NULL) {
                if(strncmp(line,"END_OF_LS",9)==0){
                    return 0;
                }
                printf("%s\n", line); // 打印每个文件名
                line = strtok(NULL, "\n"); // 继续查找下一个文件名

            }
            
        }
    }
    return 0;
}


